gh-pages set-up

Some of you have already been working in blogdown. But there are other ways that you can make your RMarkdown materials available for others to see. We’ll create a repo that contains .Rmd and knitted .html files that we can share as web pages!

Note: The gh-pages section of this lab is inspired by materials from Dr. Julia Lowndes (https://jules32.github.io/).

Julia has written a post on how to make website with RMarkdown and GitHub here: https://jules32.github.io/rmarkdown-website-tutorial/

Follow along with these steps to make your gh-pages branch:

In that .Rmd:

0. Attach packages

# For general stuff:
library(tidyverse)
library(janitor)
library(lubridate)
library(here)
library(paletteer)

# For ts stuff: 
library(tsibble)
library(fable)
library(feasts) # Possibly get dev version (gg_season issue): remotes::install_github("tidyverts/feasts")
library(forecast)

# For spatial stuff: 
library(sf)
library(tmap)
library(mapview)

Monthly US energy consumption (renewables)

1. Get the data

We’ll explore, then forecast, US energy consumption and production by renewables source. Get the data from renewables_cons_prod.csv:

us_renew <- read_csv(here("data", "renewables_cons_prod.csv")) %>% 
  clean_names()

Explore the data frame:

# View(us_renew)
# names(us_renew)
# unique(us_renew$description)

We’ll focus on consumption data.

Clean up data

  • Convert description to all lowercase
  • Only keep observations for “consumption”
  • Remove any “total” observations
renew_clean <- us_renew %>% 
  mutate(description = str_to_lower(description)) %>% 
  filter(str_detect(description, pattern = "consumption")) %>% 
  filter(!str_detect(description, pattern = "total"))

Convert yyyymm column to tsibble with lubridate

renew_date <- renew_clean %>% 
  mutate(month = lubridate::parse_date_time(yyyymm, "ym")) %>% 
  mutate(month = yearmonth(month)) %>% #coerce to `yearmonth` format
  mutate(value = as.numeric(value)) %>% 
  drop_na(month, value)

Make a ggplot

Make, then save, the base line plot as renew_gg:

renew_gg <- ggplot(data = renew_date, aes(x = month, y = value, group = description)) +
  geom_line(aes(color = description)) +
  theme_minimal() +
  scale_y_continuous(limits = c(0, 350))

Now try updating your color palette using options from paletteer. Use View(palettes_d_names) to see all of the discrete scale options. We’ll want a palette with at least 7 colors (length >= 7). Find a name of a palette that you like, then update your graph by adding scale_color_paletteer_d("package::palette"). Like, if I want to use the calecopal::figmtn palette, I’d add:

renew_gg + scale_color_paletteer_d("calecopal::figmtn")

Try some out!

renew_gg +
  scale_color_paletteer_d("calecopal::figmtn")

Have some fun trying out different color palettes.

Coerce to a tsibble:

renew_ts <- as_tsibble(renew_date, key = description, index = month)

Look at the data in a few different ways:

renew_ts %>% autoplot(value)

renew_ts %>% gg_subseries(value)

renew_ts %>% gg_season(value)

Get just the wind energy consumption data:

hydro_ts <- renew_ts %>% 
  filter(description == "hydroelectric power consumption")

# Explore: 
hydro_ts %>% autoplot(value)

hydro_ts %>% gg_subseries(value)

hydro_ts %>% gg_season(value)

Calculate summary data by time using index_by()

What if we want to calculate consumption by quarter? We’ll use index_by() to tell R which “windows” to calculate a value with in.

Quarterly:

hydro_quarterly <- hydro_ts %>% 
  index_by(year_qu = ~ yearquarter(.)) %>% # monthly aggregates
  summarise(
    avg_consumption = mean(value)
  )

Or annually:

hydro_annual <- hydro_ts %>% 
  index_by(annual = ~year(.)) %>% 
  summarize(
    avg_consumption = mean(value)
  )

ggplot(data = hydro_annual, aes(x = annual, y = avg_consumption)) +
  geom_line(color = "darkorange") +
  geom_smooth(color = "purple",
              size = 0.2,
              linetype = "dashed",
              fill = "purple",
              alpha = 0.2) +
  theme_minimal()

And if you have higher interval data (e.g. hourly), then you can calculate summaries by week, month, etc. using functions from tsibble like:

  • yearweek
  • yearmonth

Decompose the hydro consumption ts data

First, let’s check the decomposition (STL):

# Find STL decomposition
dcmp <- hydro_ts %>%
  model(STL(value ~ season(window = Inf)))

# View the components
# components(dcmp)

# Visualize the decomposed components
components(dcmp) %>% autoplot() +
  theme_minimal()

Forecast future hydro power consumption

hydro_model <- hydro_ts %>%
  model(
    arima = ARIMA(value)
  ) %>%
  fabletools::forecast(h = "2 years")

hydro_model %>% autoplot(filter(hydro_ts, year(month) > 2010), level = NULL)

Map-of-the-day

A world map with bubbles!

# Get spatial data: 
world <- read_sf(dsn = here("data","TM_WORLD_BORDERS_SIMPL-0.3-1"), layer = "TM_WORLD_BORDERS_SIMPL-0.3") %>% clean_names()

# Initial plot
plot(world)

# ggplot (static)
world_base <- ggplot(data = world) +
  geom_sf(aes(fill = pop2005),
          color = NA) + 
  scale_fill_paletteer_c("viridis::viridis") +
  theme_minimal()

world_base

# Let's crop it: 
world_base +
  coord_sf(xlim = c(-20, 50), ylim = c(-40, 40), expand = FALSE)

Making this a web page:

Since you are working in a gh-pages branch, this can be a webpage!

End Lab Week 5